package com.h3c.ptability.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.cache.UserCache;
import com.h3c.ptability.dto.PtDeptDTO;
import com.h3c.ptability.entity.PtAllWorker;
import com.h3c.ptability.entity.PtApp;
import com.h3c.ptability.entity.PtUserDept;
import com.h3c.ptability.service.IPtAllWorkerService;
import com.h3c.ptability.service.IPtUserDeptService;
import com.h3c.ptability.dto.PtUserDeptDTO;
import com.h3c.ptability.utils.AppBusinessException;
import com.h3c.ptability.utils.ReturnCodeData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Copyright (C), 2022-2022, h3c
 * FileName: PtUserDeptController
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_user_dept controller
 * Version: 1.0
 */
@Api(tags = " PtUserDept 接口")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/ptUserDept", produces = "application/json;charset=utf-8")
public class PtUserDeptController {

    @Resource
    private IPtUserDeptService ptUserDeptService;


    /**
     * 查询用户有权限的部门id列表
     *
     * @param appId
     * @return
     */
    @GetMapping("/getUserDeptPermission")
    public Result getUserDeptPermission(@RequestParam(required = false) String userId,
                                        @RequestParam(required = false) String domainAccount,
                                        @RequestParam String appId) {

        return new Result(true, 20000, "查询用户有权限的部门id列表成功", ptUserDeptService.getUserDeptPermission(userId, domainAccount, appId));
    }


    @GetMapping("/getUserDeptPermissionWithLevel")
    public Result getUserDeptPermissionWithDeptLevel(@RequestParam("domainAccount") String domainAccount,
                                                     @RequestParam("appId") String appId,
                                                     @RequestParam("deptLevel") Integer deptLevel) {

        return new Result(true, StatusCodeEnum.NORMAL, ptUserDeptService.getUserDeptPermissionWithDeptLevel(appId, domainAccount, deptLevel));
    }

    /**
     * 查询用户有权限的部门id列表  (hr-sign专用)
     *
     * @param appId
     * @return
     */
    @GetMapping("/getUserDeptPermissionHrSign")
    public Result getUserDeptPermissionHrSign(@RequestParam(required = false) String userId,
                                              @RequestParam(required = false) String domainAccount,
                                              @RequestParam String appId) {

        return new Result(true, 20000, "查询用户有权限的部门id列表成功", ptUserDeptService.getUserDeptPermissionHrSign(userId, domainAccount, appId));
    }

    /**
     * 查询用户有权限的部门id列表 ——>合同平台
     *
     * @param appId
     * @return
     */
    @GetMapping("/getUserDeptPermissionforHrSign")
    public Result getUserDeptPermissionforHrSign(@RequestParam(required = false) String userId,
                                                 @RequestParam(required = false) String domainAccount,
                                                 @RequestParam String appId) {

        return new Result(true, 20000, "查询用户有权限的部门id列表成功", ptUserDeptService.getUserDeptPermissionforHrSign(userId, domainAccount, appId));
    }


    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtUserDeptDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public Result queryFuzzyListForPage(@ModelAttribute PtUserDeptDTO paramsDTO,
                                        @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                        @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtUserDeptDTO> resultDTOList = ptUserDeptService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtUserDeptDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }

    /**
     * @title: queryListForPage
     * @description: 列表分页精确查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtUserDeptDTO", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public Result queryListForPage(@ModelAttribute PtUserDeptDTO paramsDTO,
                                   @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                   @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtUserDeptDTO> resultDTOList = ptUserDeptService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtUserDeptDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页查询成功", pageInfo);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtUserDept", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PtUserDeptDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptUserDeptService.insertDto(paramsDTO);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtUserDept", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody PtUserDeptDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptUserDeptService.updateDto(paramsDTO);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据修改成功", result);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtUserDept", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody PtUserDeptDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptUserDeptService.deleteDto(paramsDTO);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据删除", result);
    }

    /**
     * 根据appId和部门id获取PtUserDept对象。 远程调用
     * @param appId
     * @param deptId
     * @return
     */
    @GetMapping("/getUserDept")
    public PtUserDept getUserDept(@RequestParam("appId") String appId,
                                          @RequestParam("deptId") String deptId){
        return ptUserDeptService.getOne(new QueryWrapper<PtUserDept>().lambda()
                .eq(PtUserDept::getAppId, appId)
                .eq(PtUserDept::getDeptId, Integer.valueOf(deptId)));
    }

    /**
     * 获取有权限的部门树
     * @param appId
     * @param workerId
     * @param flag
     * @return
     */
    @RequestMapping(value = "/getAuthorizeChildTree", method = RequestMethod.GET)
    public Result<List<PtDeptDTO>> getAuthorizeChildTree(@RequestParam(name = "appId") String appId, @RequestParam(name = "workerId") String workerId, @RequestParam("flag") Boolean flag){
        PtDeptDTO root = ptUserDeptService.getAuthorizeChildTree(appId, workerId, flag);
        List<PtDeptDTO> ptDeptDTOS = ptUserDeptService.changeRootToChildList(root);
        return new Result<>(true, StatusCodeEnum.SEARCH_SUCCESS, ptDeptDTOS);
    }

}
